package com.honghea.rxhttp.exception;


import com.google.gson.JsonParseException;

import org.apache.http.conn.ConnectTimeoutException;
import org.json.JSONException;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.text.ParseException;

import retrofit2.HttpException;

public class ExceptionEngine {


    private static final int JSON_ANALYSIS_ERROR = 2000;
    private static final int HTTP_ERROR = 2001;
    private static final int CONNECT_ERROR = 2002;
    private static final int SERVER_ERROR = 2003;
    private static final int UNKNOWN_ERROR = 2004;

    private static final int UNAUTHORIZED = 401;
    private static final int FORBIDDEN = 403;
    private static final int NOT_FOUND = 404;
    private static final int REQUEST_TIMEOUT = 408;
    private static final int INTERNAL_SERVER_ERROR = 500;
    private static final int BAD_GATEWAY = 502;
    private static final int SERVICE_UNAVAILABLE = 503;
    private static final int GATEWAY_TIMEOUT = 504;

    public static ApiException exceptionHandler(Throwable e) {
        ApiException ex;
        if (e instanceof SocketTimeoutException
                || e instanceof ConnectTimeoutException
                || e instanceof ConnectException) {
            ex = new ApiException(e, CONNECT_ERROR);
            ex.msg = "Connect error: " + e.getMessage();
            return ex;
        }
        else if (e instanceof HttpException) {
            HttpException httpException = (HttpException) e;
            ex = new ApiException(e, HTTP_ERROR);
            switch (httpException.code()) {
                case UNAUTHORIZED:
                    ex.msg = "Http error: "+"UNAUTHORIZED";
                    break;
                case FORBIDDEN:
                    ex.msg = "Http error: "+"FORBIDDEN";
                    break;
                case NOT_FOUND:
                    ex.msg = "Http error: "+"NOT FOUND";
                    break;
                case REQUEST_TIMEOUT:
                    ex.msg = "Http error: "+"REQUEST TIMEOUT";
                    break;
                case GATEWAY_TIMEOUT:
                    ex.msg = "Http error: "+"GATEWAY TIMEOUT";
                    break;
                case INTERNAL_SERVER_ERROR:
                    ex.msg = "Http error: "+"INTERNAL SERVER ERROR";
                    break;
                case BAD_GATEWAY:
                    ex.msg = "Http error: "+"BAD GATEWAY";
                    break;
                case SERVICE_UNAVAILABLE:
                    ex.msg = "Http error: "+"SERVICE UNAVAILABLE";
                    break;
                default:
                    ex.msg = "Http error: "+"NETWORK ERROR";
                    break;
            }
            return ex;
        }
        else if (e instanceof JSONException
                || e instanceof JsonParseException
                || e instanceof ParseException) {
            ex = new ApiException(e, JSON_ANALYSIS_ERROR);
            ex.msg = "JSON ANALYSIS ERROR: " + e.getMessage();
            return ex;
        } else if (e instanceof ApiException) {
            return (ApiException) e;
        } else {
            ex = new ApiException(e, UNKNOWN_ERROR);
//            ex.msg = "UNKNOWN ERROR" + e.getMessage(); //todo 网络请求错误
            ex.msg = "Network request error";
            return ex;
        }
    }

}
